package services;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.SqlSession;

import play.libs.Codec;
import vos.OperatorSearchVo;
import vos.OperatorSearchVo;

import com.aton.db.SessionFactory;

import models.Operator;
import models.Operator;
import models.User;
import models.mappers.OperatorMapper;
import models.mappers.OperatorMapper;
import models.mappers.StandardMapper;
import models.mappers.UserMapper;

public class OperatorService {
	/**
	 * 根据id查找操作员
	 *
	 * @Title: findByID 
	 * @param operatorid
	 * @return
	 * @return: Operator
	 */
	public static Operator findById(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            return mapper.findById(operatorid);
        } finally {
            ss.close();
        }
	}
	/**
	 * 修改密码
	 *
	 * @Title: updatePass 
	 * @param oldoper 新密码
	 * @return
	 * @return: boolean
	 */
	public static boolean updatePass(Operator oldoper) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            mapper.updatePass(oldoper);
            ss.commit();
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        }
		finally {
            ss.close();
        }
		return true;
	}
	/**
	 * 根据条件统计操作员数量
	 *
	 * @Title: countOperator 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countOperator(OperatorSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            return mapper.countOperator(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 根据条件查询操作员列表
	 *
	 * @Title: listOperator 
	 * @param vo
	 * @return
	 * @return: List<Operator>
	 */
	public static List<Operator> listOperator(OperatorSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            return mapper.listOperator(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 批量启用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @param operatorid 
	 * @return: void
	 */
	public static Boolean betchEnable(String ids, String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            mapper.betchEnable(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量停用
	 *
	 * @Title: betchEnable 
	 * @param ids 选中记录id
	 * @return: void
	 */
	public static Boolean betchDisable(String ids,String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,Object> map=new HashMap<>();
			map.put("listId", listid);
			Date date=new Date();
			map.put("modifytime", date);
			map.put("modifyname", operatorid);
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            mapper.betchDisable(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 批量删除
	 *
	 * @Title: betchDel 
	 * @param ids
	 * @return
	 * @return: boolean
	 */
	public static boolean betchDel(String ids) {
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			Map<String,List<String>> map=new HashMap<>();
			map.put("listId", listid);
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            mapper.betchDel(map);
            ss.commit();
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 删除操作员
	 *
	 * @Title: delOperator 
	 * @param id 操作员id
	 * @return
	 * @return: boolean
	 */
	public static boolean delOperator(String id) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
	        mapper.delOperator(id);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		
		return true;
	}
	/**
	 * 验证编号是否存在
	 *
	 * @Title: checkCode 
	 * @param operatorid 操作员编号
	 * @return
	 * @return: boolean
	 */
	public static boolean checkCode(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
	        String operator= mapper.checkCode(operatorid);
	        if(StringUtils.isEmpty(operator)){
	        	return false;
	        }
	        else{
	        	return true;
	        }
		}catch(Exception e){
			e.printStackTrace();
			return true;
		}finally{
			ss.close();
		}
	}
	/**
	 * 根据id获取操作员信息
	 *
	 * @Title: getById 
	 * @param operatorid 操作员id
	 * @return
	 * @return: Operator
	 */
	public static Operator getById(String operatorid) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSession();
		try{
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
			return mapper.getById(operatorid);
		}finally{
			ss.close();
		}
	}
	/**
	 * 保存操作员信息
	 *
	 * @Title: saveOperator 
	 * @param operator 操作员对象
	 * @return
	 * @return: boolean
	 */
	public static boolean saveOperator(Operator operator) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			String mobile=operator.operatormobile.trim();
			String pass=mobile.substring(mobile.length()-6,mobile.length());
			operator.salt=String.valueOf(Math.random()).substring(2, 8);
			operator.password=Codec.hexMD5(pass+operator.salt);
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
			Date date=new Date();
			operator.createtime=date;
	        mapper.saveOperator(operator);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 更新操作员信息
	 *
	 * @Title: updateOperator 
	 * @param operator
	 * @return
	 * @return: boolean
	 */
	public static boolean updateOperator(Operator operator) {
		// TODO Auto-generated method stub
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try{
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
			Date date=new Date();
			operator.modifytime=date;
	        mapper.updateOperator(operator);
	        ss.commit();
		}
		catch(Exception e){
			e.printStackTrace();
			return false;
		}finally{
			ss.close();
		}
		return true;
	}
	/**
	 * 重置操作员密码
	 *
	 * @Title: resetPass 
	 * @param oper 操作员
	 * @return: void
	 */
	public static boolean resetPass(Operator oper) {
		// TODO Auto-generated method stub
		String pass=oper.operatormobile.substring(oper.operatormobile.length()-6,oper.operatormobile.length());
		String password=Codec.hexMD5(pass+oper.salt);
		if(password.equals(oper.password)){
			return true;
		}
		oper.password=password;
		SqlSession ss = SessionFactory.getSqlSessionWithoutAutoCommit();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
			mapper.rePassword(oper);
            ss.commit();
            return true;
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取启用的操作员列表
	 *
	 * @Title: listUseOperator 
	 * @param vo
	 * @return
	 * @return: List<Operator>
	 */
	public static List<Operator> listUseOperator(OperatorSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            return mapper.listUseOperator(vo);
        } finally {
            ss.close();
        }
	}
	/**
	 * 获取启用的操作员数量
	 *
	 * @Title: countUseOperator 
	 * @param vo
	 * @return
	 * @return: long
	 */
	public static long countUseOperator(OperatorSearchVo vo) {
		SqlSession ss = SessionFactory.getSqlSession();
		try {
			OperatorMapper mapper = ss.getMapper(OperatorMapper.class);
            return mapper.countUseOperator(vo);
        } finally {
            ss.close();
        }
	}
}
